package com.urban.pooling.service.google;

import java.util.ArrayList;

import org.json.JSONException;
import org.json.JSONObject;

import com.urban.pooling.domain.GeoPoint;

public class Steps {
	
	private GeoPoint start;
	private GeoPoint end;
	private String travelMode;
	private String duration;
	private String distance;
	private String instructions;
	
	private ArrayList<GeoPoint> stepLine;
	
	public Steps(JSONObject obj){
		stepLine = new ArrayList<GeoPoint>();
		parseStep(obj);
	}
	
	public GeoPoint getStepStartPosition(){
		return start;
	}
	
	public GeoPoint getEndStepPosition(){
		return end;
	}
	
	public String getStepTravelMode(){
		return travelMode;
	}
	
	public String getStepDuration(){
		return duration;
	}
	
	public String getStepDistance(){
		return distance;
	}
	
	public String getStepInstructions(){
		return instructions;
	}
	
	public ArrayList<GeoPoint> getSpetLinePoints(){
		return stepLine;
	}

	private void parseStep(JSONObject step){
		try{
			travelMode = step.getString("travel_mode");
			
			if(!step.isNull("start_location")){
				JSONObject pos = step.getJSONObject("start_location");
				start = new GeoPoint(pos.getDouble("lat"),pos.getDouble("lng"));
			}
			
			if(!step.isNull("end_location")){
				JSONObject pos = step.getJSONObject("end_location");
				end = new GeoPoint(pos.getDouble("lat"),pos.getDouble("lng"));
			}
			
			if(!step.isNull("duration")){
				JSONObject pos = step.getJSONObject("duration");
				duration = pos.getString("text");
			}
			
			if(!step.isNull("distance")){
				JSONObject pos = step.getJSONObject("distance");
				distance = pos.getString("text");
			}
			
			if(!step.isNull("polyline")){
				JSONObject pos = step.getJSONObject("polyline");
				decodePoly(pos.getString("points"));
//				Log.d("Step count", String.valueOf(stepLine.size()));
			}
			
			instructions = step.getString("html_instructions");
		}catch(JSONException e){
			e.printStackTrace();
		}
		
	}
	
	private void decodePoly(String encoded) {
        int index = 0, len = encoded.length();
        int lat = 0, lng = 0;
        while (index < len) {
            int b, shift = 0, result = 0;
            do {
                b = encoded.charAt(index++) - 63;
                result |= (b & 0x1f) << shift;                 
                shift += 5;             
            } while (b >= 0x20);
            int dlat = ((result & 1) != 0 ? ~(result >> 1) : (result >> 1));
            lat += dlat;
            shift = 0;
            result = 0;
            do {
                b = encoded.charAt(index++) - 63;
                result |= (b & 0x1f) << shift;  
                shift += 5;             
            } while (b >= 0x20);
            int dlng = ((result & 1) != 0 ? ~(result >> 1) : (result >> 1));
            lng += dlng;
 
            GeoPoint position = new GeoPoint((double) lat / 1E5, (double) lng / 1E5);
//            Log.d("Lat", String.valueOf(position.latitude));
//            Log.d("Lng", String.valueOf(position.longitude));
            stepLine.add(position);
        }
    }
}
